package com.qfedu.web;

import com.qfedu.dao.AccountDao;
import com.qfedu.pojo.Account;
import com.qfedu.pojo.MyProduct;
import com.qfedu.pojo.Product;
import com.qfedu.pojo.User;
import com.qfedu.service.AccountService;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@WebServlet("/productServlet")
public class ProductServlet extends BaseServlet{
    private ProductService productService = new ProductServiceImpl();
    private AccountService accountService = new AccountServiceImpl();
    public void findMyProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
        if (user!=null){
            String phone = user.getPhone();
            Account account = accountService.queryIdcodeByPhone(phone);
            String idcode = account.getIdcode();
            List<MyProduct> productList = productService.findMyProduct(idcode);
            if(productList.size()==0){
                request.getRequestDispatcher("/pages/product/nullproduct.jsp").forward(request,response);
            }else {
                request.getSession().setAttribute("productsList",productList);
                System.out.println(productList);
                request.getRequestDispatcher("/pages/product/myproduct.jsp").forward(request,response);
            }

        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }
    }
    public void findAllProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> products = productService.findAllProducts();
        request.getSession().setAttribute("products",products);
        System.out.println(products);
        response.sendRedirect("/pages/product/products.jsp");
    }
    public void findProductById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String where = request.getParameter("where");//判断当前是我的产品，还是购买产品
        if (where.equals("1")){
            String id = request.getParameter("pid");
            int pid = Integer.parseInt(id);
            Product product = productService.findProductById(pid);
            if (product!=null){
                request.getSession().setAttribute("MyProduct",product);
                request.getRequestDispatcher("/pages/product/payProductDetail.jsp").forward(request,response);
            }else {
                System.out.println("没有当前产品");
            }
        }else {
            String only = request.getParameter("id");
            MyProduct myProduct = productService.findMyProductByOnly(Integer.parseInt(only));
            if (myProduct!=null){
                request.getSession().setAttribute("myProduct",myProduct);
                request.getRequestDispatcher("/pages/product/backProductDetail.jsp").forward(request,response);
            }else {
                System.out.println("您产品列表无此产品");
            }

        }
    }

    public void pay(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
        if (user != null) {
            String phone = user.getPhone();
            Account account = accountService.queryIdcodeByPhone(phone);
            if (account!=null){
                String idcode = account.getIdcode();
                if (idcode != null) {
                    //找到当前电子余额
                    //ProductService productService = new ProductServiceImpl();
                    Account accountByIdcode = accountService.queryAccountByIdcode(idcode);
                    /*BigDecimal leftMoney = accountByIdcode.getMoney();*/
                    request.getSession().setAttribute("MyAccount",accountByIdcode);
                    Product myProduct = (Product)request.getSession().getAttribute("MyProduct");
                    System.out.println(myProduct);
                    response.sendRedirect("/pages/product/payproduct.jsp");
                }
            }else {
                response.sendRedirect("/pages/user/openaccount.jsp");
            }
        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }
    }
    public void isPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Account myAccount = (Account)request.getSession().getAttribute("MyAccount");
        String password = request.getParameter("password");
        String paypassword = myAccount.getPaypassword();
        if (!paypassword.equals(password)){
            request.getSession().setAttribute("msg","当前支付密码不正确");
            request.getRequestDispatcher("/pages/product/payproduct.jsp").forward(request,response);
        }else {
            //找到当前商品的单价
            String id = request.getParameter("pid");
            int pid = Integer.parseInt(id);
            Product productById = productService.findProductById(pid);
            BigDecimal onePrice = productById.getPrice();
            //当前商品的份数
            String pum = request.getParameter("pnum");
            int num = Integer.parseInt(pum);
            BigDecimal pnum = new BigDecimal(num);
            BigDecimal totalPrice = onePrice.multiply(pnum);
            //判断当前电子余额和产品单价价格对比
            BigDecimal leftMoney = myAccount.getMoney();
            if (leftMoney.compareTo(totalPrice) < 0) {//能够购买
                request.getSession().setAttribute("msg","当前余额不足");
                request.getRequestDispatcher("/pages/product/payproduct.jsp").forward(request,response);
            }
            BigDecimal mon = leftMoney.subtract(totalPrice);//剩余的钱
            String idcode = myAccount.getIdcode();
            int i = productService.payProduct(idcode,pid,pnum, mon);//当前支付人的身份证，当前购买的商品id，购买后剩余的钱
            if (i==1){
                request.getRequestDispatcher("/pages/product/paysuccess.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("/pages/product/payfaild.jsp").forward(request,response);
            }
        }
    }
    public void back(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
        if (user != null) {
            String phone = user.getPhone();
            Account account = accountService.queryIdcodeByPhone(phone);
            String idcode = account.getIdcode();
            if (idcode != null) {
                //找到当前电子余额
                ProductService productService = new ProductServiceImpl();
                Account accountByIdcode = accountService.queryAccountByIdcode(idcode);
                BigDecimal leftMoney = accountByIdcode.getMoney();
                request.getSession().setAttribute("myAccount",accountByIdcode);
                MyProduct myProduct = (MyProduct)request.getSession().getAttribute("myProduct");
                System.out.println(myProduct);
                response.sendRedirect("/pages/product/backproduct.jsp");
            }
        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }
    }
    public void isBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //找到当前剩余的钱数
        Account myAccount = (Account)request.getSession().getAttribute("myAccount");
        String password = request.getParameter("password");
        String paypassword = myAccount.getPaypassword();
        if (!paypassword.equals(password)){
            request.getSession().setAttribute("errorMsg","当前支付密码不正确");
            request.getRequestDispatcher("/pages/product/backproduct.jsp").forward(request,response);
        }
        BigDecimal leftMoney = myAccount.getMoney();
        //找到当前商品的份数
        MyProduct myProduct = (MyProduct) request.getSession().getAttribute("myProduct");
        Integer pum = myProduct.getPnum();
        BigDecimal pnum = new BigDecimal(pum);
        Integer id = myProduct.getId();
        //当前要赎回的商品的份数
        String pnumber = request.getParameter("pnum");
        if (pnumber!=null){
            int i = Integer.parseInt(pnumber);
            BigDecimal bigDecimal = new BigDecimal(i);
            if (pnum.compareTo(bigDecimal)>=0){//可以赎回
                int j = 0;//当前全部赎回
                if (pnum.compareTo(bigDecimal)>0){
                    j=1;//当前没全赎回
                }
                //获取当前商品单价
                BigDecimal price = myProduct.getPrice();
                //获取收益率
                Double prate = myProduct.getPrate();
                //获取购买日期
                Date payDate = myProduct.getPayDate();
                long payDateTime = payDate.getTime();
                //获取当前赎回日期
                Date date = new Date();
                long backDateTime = date.getTime();
                //获取期限
                int m = myProduct.getLimitDate();
                long limitDateTime = (long)m * 24 * 60 * 60 * 1000;
                //单价*份数*(1+收益率*买入日期/期限)-单价*1/10
                    //份数*(1+收益率*买入日期/期限)
                BigDecimal bigDecimal1 = new BigDecimal(i*(1.00+prate*(backDateTime-payDateTime)/limitDateTime));
                    //单价*份数*(1+收益率*买入日期/期限)-单价*1/10
                BigDecimal mon = price.multiply(bigDecimal1.subtract(new BigDecimal(0.1))).add(leftMoney);
                //剩余份数
                BigDecimal left = pnum.subtract(bigDecimal);
                int leftNum = left.intValue();
                //执行赎回操作
                String idcode = myAccount.getIdcode();
                int o = productService.backProduct(idcode,id,mon,j,leftNum);//当前支付人的身份证，当前购买的商品id，购买后剩余的钱
                if (o==1){
                    request.getSession().setAttribute("errorMsg","");
                    request.getRequestDispatcher("/pages/product/backsuccess.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/pages/product/backfaild.jsp").forward(request,response);
                }

            }else {
                request.getSession().setAttribute("errorMsg","当前赎回份数不能大于购买份数");
                request.getRequestDispatcher("/pages/product/backproduct.jsp").forward(request,response);
            }
        }else {
            request.getSession().setAttribute("errorMsg","赎回份数不能为空");
            request.getRequestDispatcher("/pages/product/backproduct.jsp").forward(request,response);
        }
    }
}
